﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using anks.Components;

namespace pygs.UI
{
    public abstract class CustomUI : Component
    {
        public delegate void UICommandHandler(CustomUI sender, UICommand command);

        protected virtual void DoCommand(UICommand command)
        {
            UICommandHandler h = Command;
            if (h != null)
                h(this, command);
        }

        protected override void InitilizeComponent()
        {
            base.InitilizeComponent();
            RegisterUIStatusPerformers();
        }


        private bool _isUIRunned = false;
        public bool isUIRunned { get { return _isUIRunned; } }

        protected abstract void DoRunUI();
        public void RunUI()
        {
            _isUIRunned = true;
            DoRunUI();
            _isUIRunned = false;
        }

        protected abstract class CustomUIStatusPerformer
        {
            public abstract bool Perform(UIStatus status);
            public abstract bool IsAcceptedFor(UIStatus status);
        }

        protected abstract class UIStatusPerformer<T> : CustomUIStatusPerformer where T : UIStatus
        {
            protected abstract bool DoPerform(T status);
            public override bool Perform(UIStatus status)
            {
                return DoPerform((T)status);
            }
            public override bool IsAcceptedFor(UIStatus status)
            {
                return (status is T);
            }
        }

        protected class UIStatusPerformerByFunc<T> : UIStatusPerformer<T> where T : UIStatus
        {

            private Func<T, bool> performer;

            public UIStatusPerformerByFunc(Func<T, bool> performer)
            {
                this.performer = performer;
            }
              
            protected override bool DoPerform(T status)
            {
                return performer(status);
            }
        }

        private List<CustomUIStatusPerformer> UIStatusPerformerRegistry = new List<CustomUIStatusPerformer>();

        protected void RegisterUIStatusPerformer<T>(Func<T, bool> performer) where T : UIStatus
        {
            RegisterUIStatusPerformer<T>(new UIStatusPerformerByFunc<T>(performer));
        }

        protected void RegisterUIStatusPerformer<T>(UIStatusPerformer<T> performer) where T : UIStatus
        {
            UIStatusPerformerRegistry.Add(performer);
        }

        protected virtual void RegisterUIStatusPerformers()
        {
        }

        public virtual void PerformStatus(UIStatus status)
        {
            if (isUIRunned)
                foreach (var performer in UIStatusPerformerRegistry.TakeWhile(p => p.IsAcceptedFor(status)).Reverse())
                    if (performer.Perform(status))
                        break;
        }

        public event UICommandHandler Command;
    }
}
